package com.fjh.controller.bus;

import com.fjh.common.Result;
import com.fjh.domain.BusCar;
import com.fjh.dto.CarDto;
import com.fjh.service.CarService;
import com.fjh.vo.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/buscar")
public class CarController {

    @Autowired
    private CarService carService;


    /**
     * 分页查询
     *
     * @param carDto
     * @return
     */
    @GetMapping("/findByPage.do")
    @RequiresPermissions("bus:car:list")
    public Result findByPage(CarDto carDto) {
        PageInfo<BusCar> carPageInfo = carService.findByPage(carDto);
        return Result.ok(carPageInfo);
    }

    /**
     * 新增数据
     *
     * @param carDto
     * @return
     */
    @PostMapping("/add.do")
    @RequiresPermissions("bus:car:add")
    public Result add(@RequestBody CarDto carDto) {
        Boolean isOk = carService.save(carDto);
        return isOk ? Result.ok() : Result.fail();
    }

    /**
     * 修改数据
     *
     * @param carDto
     * @return
     */
    @PostMapping("/update.do")
    @RequiresPermissions("bus:car:update")
    public Result update(@RequestBody CarDto carDto) {
        Boolean isOk = carService.updateById(carDto);
        return isOk ? Result.ok() : Result.fail();
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete.do/{ids}")
    @RequiresPermissions("bus:car:delete")
    public Result delete(@PathVariable Integer[] ids) {
        Boolean isOk = carService.batchDelete(ids);
        return isOk ? Result.ok() : Result.fail();
    }

    /**
     * 租车的时
     * @param carId 车辆的id
     * @param idCard 客户的身份证号
     * @return 是否租车成功
     */
    @PostMapping("/rent.do")
    public Result rentCar(Integer carId,String idCard,String startTime ,String endTime){
        Boolean isOk = carService.rentCar(carId ,idCard,startTime,endTime) ;
        return isOk ?  Result.ok() : Result.fail();
    }
}
